home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 894 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. From: "Wil Evers" <wil@ittpub.nl>
  2. Message-ID: <009A0041E919D6E0.3D60EEDA@ittpub.nl>
  3. X-Original-Date: Thu, 28 Mar 96 15:15:49 WET
  4. Path: in2.uu.net!bounce-back
  5. Date: 28 Mar 96 16:20:31 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Newsgroups: comp.std.c++
  9. Return-Path: <daemon@meeker.UCAR.EDU>
  10. Subject: Re: initialization of nonlocal static objects
  11. X-Vms-Mail-To: IN%"std-c++@ncar.ucar.edu"
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMVq8fuEDnX0m9pzZAQGFRwGAm7rpieiXNI4SLkXWFn6iBHvrBC8i1xb1
  14.     lOnmxa/D0Ruym75qXGUN95ZBF4UACjsc
  15.     =O64g
  16.  
  17. In article <KANZE.96Mar27134634@gabi.gabi-soft.fr> kanze@gabi-soft.fr (J.
  18. Kanze) writes:
  19.  
  20. >     class X
  21. >     {
  22. >     public :
  23. >         enum DontInit { dontInit } ;
  24. >                         X( DontInit )   {}
  25. >                         X() : i( 5 )    {}
  26. >     private :
  27. >         int             i ;
  28. >     } ;
  29. > Objects to be initialized by the nifty_counter would be defined thus:
  30. >     X               obj( X::dontInit ) ;
  31. > Nifty counter then becomes:
  32. >     class NiftyCounter
  33. >     {
  34. >         static int          cnt ;
  35. >     public :
  36. >                             NiftyCounter()
  37. >         {
  38. >             if ( cnt == 0 )
  39. >                 new( &obj ) X ;
  40. >             cnt ++ ;
  41. >         }
  42. >     } ;
  43. > (Note that for NiftyCounter to work, it is absolutely essential that the
  44. > definition of the object invoke a no-op constructor, since the object
  45. > may in fact already be constructed.)
  46. > As you can see, this works equally well for const objects.
  47.  
  48. I don't think so. For this to work on a const object, you would at least have
  49. to cast away its constness - after all, the standard placement new operator
  50. takes a void * as its second argument, not a const void *.
  51.  
  52. In my opinion, the `dummy constructor' technique is fundamentally bad. It
  53. requires that all base classes and member variables have dummy constructors
  54. too, so it is not always applicable.
  55.  
  56. When I use the nifty counter idiom, I normally construct the global
  57. object in a suitably aligned raw memory buffer (a character array).
  58. Users access the constructed object through a properly typed global
  59. reference pointing to the raw memory buffer.
  60.  
  61. - Wil
  62. ---
  63. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  64. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  65. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  66. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  67. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  68.